--[[--------------------------------------------------------------------
	GridCodeStatusMana.lua
	GridCodeStatus module for tracking unit mana.
----------------------------------------------------------------------]]

local _, GridCode = ...
local L = GridCode.L

local GridCodeRoster = GridCode:GetModule("GridCodeRoster")
local GridCodeStatus = GridCode:GetModule("GridCodeStatus")

local GridCodeStatusMana = GridCodeStatus:NewModule("GridCodeStatusMana")
GridCodeStatusMana.menuName = L["Mana"]

GridCodeStatusMana.defaultDB = {
	debug = false,
	alert_lowMana = {
		text = L["Low Mana"],
		enable = true,
		color = { r = .5, g = .5, b = 1, a = 1 },
		priority = 40,
		threshold = 10,
		range = false,
	},
}

GridCodeStatusMana.options = false

local low_manaOptions = {
	["threshold"] = {
		type = "range", width = "double",
		name = L["Mana threshold"],
		desc = L["Set the percentage for the low mana warning."],
		max = 100,
		min = 0,
		step = 1,
		get = function()
			return GridCodeStatusMana.db.profile.alert_lowMana.threshold
		end,
		set = function(_, v)
			GridCodeStatusMana.db.profile.alert_lowMana.threshold = v
		end,
	},
}

function GridCodeStatusMana:PostInitialize()
	self:RegisterStatus("alert_lowMana", L["Low Mana warning"], low_manaOptions, true)
end

function GridCodeStatusMana:OnStatusEnable(status)
	if status ~= "alert_lowMana" then return end

	self:RegisterMessage("GridCode_UnitJoined")

	self:RegisterEvent("UNIT_POWER", "UpdateUnit")
	self:RegisterEvent("UNIT_MAXPOWER", "UpdateUnit")
	self:RegisterEvent("UNIT_DISPLAYPOWER", "UpdateUnit")

	self:UpdateAllUnits()
end

function GridCodeStatusMana:OnStatusDisable(status)
	if status ~= "alert_lowMana" then return end

	self:UnregisterMessage("GridCode_UnitJoined")

	self:UnregisterEvent("UNIT_POWER")
	self:UnregisterEvent("UNIT_MAXPOWER")
	self:UnregisterEvent("UNIT_DISPLAYPOWER")

	self.core:SendStatusLostAllUnits("alert_lowMana")
end

function GridCodeStatusMana:PostReset()
	self:UpdateAllUnits()
end

function GridCodeStatusMana:GridCode_UnitJoined(event, guid, unitid)
	if unitid then
		self:UpdateUnit(event, unitid)
	end
end

function GridCodeStatusMana:UpdateAllUnits()
	for guid, unitid in GridCodeRoster:IterateRoster() do
		self:UpdateUnit("UpdateAllUnits", unitid)
	end
end

function GridCodeStatusMana:UpdateUnit(event, unitid)
	local guid = UnitGUID(unitid)
	if not GridCodeRoster:IsGUIDInRaid(guid) then return end
	local powerType = UnitPowerType(unitid)

	-- mana user and is alive
	if powerType == 0 and not UnitIsDeadOrGhost(unitid) then
		local cur, max = UnitPower(unitid, powerType), UnitPowerMax(unitid, powerType)

		local mana_percent = (cur / max * 100)
		local threshold = self.db.profile.alert_lowMana.threshold

		self:StatusLowMana(guid, mana_percent <= threshold)
	else
		self:StatusLowMana(guid, false)
	end
end

function GridCodeStatusMana:StatusLowMana(guid, gained)
	local settings = self.db.profile.alert_lowMana

	-- return if this option isnt enabled
	if not settings.enable then return end

	if gained then
		GridCodeStatus:SendStatusGained(guid, "alert_lowMana",
			settings.priority,
			(settings.range and 40),
			settings.color,
			settings.text,
			nil,
			nil,
			settings.icon)
	else
		GridCodeStatus:SendStatusLost(guid, "alert_lowMana")
	end
end
